class Storage {
    constructor() { }
    
    /**
     * 设置缓存
     * @param {*传入配置} params 
     * @param {*失效时长} expires 
     */
    setItem(params,expires=14) {
        const options = this.options(params,expires)

        const type = Object.prototype.toString.call(options.value)
        const arr_type = Object.prototype.toString.call([])
        const obj_type = Object.prototype.toString.call({})
        if (type === arr_type || type === obj_type) {
            options.value = JSON.stringify(options.value)
        }
        localStorage.setItem(options.name,JSON.stringify(options))
    }
    getItem(name,son=false) {
        let item = localStorage.getItem(name)
        try {
            item = JSON.parse(item)
        } catch(error) {
            console.log(error)
        }

        return this.isExpires(item,son)
    }
    /**
     * 移除缓存
     * @param {要移除的键} name 
     */
    removeItem(name) {
        localStorage.removeItem(name)
    }

    /**
     * 设置子缓存
     * @param {*键} name
     * @param {*传入配置} params 
     * @param {*失效时间} expires
     * @param {*最大数据量} maxLength 
     */
    setItemSon(name, params, expires = 14,maxLength=50) {
        let item = this.getItem(name,true) || {}

        // 父配置
        const options = this.options(item,expires)
        if (options.name === '') {
            options.name = name
            options.value = []
        } else {
            options.value = JSON.parse(options.value)
        }
        // 子配置
        const option = this.options(params,expires)

        options.value.forEach((item,index) => {
            if (item.name === option.name) {
                options.value = options.value.slice(index+1)
                return
            }
        });
        options.value.unshift(option)
        this.setItem(options,expires)
    }
    getItemSon(name) {
        let item = this.getItem(name)
        if (!item) return false
        
        const arr = item.filter(item_son => {
            return this.isExpires(item_son,true)
        });
        // this.removeItem(name)
        return arr
    }

    /**
     * 基本配置
     * @param {传入配置} params 
     * @param {*失效时长} expires 
     * @returns 
     */
    options(params,expires) {
        const obj = {
            name: '',
            value: '',
            expires: expires * 86400000,
            startTime: new Date().getTime()//记录何时将值存入，毫秒级
        }
        const options = {}
        Object.assign(options, obj, params)
        
        return options
    }


    /**
     * 判断是否失效
     * @param {*传入配置} params 
     * @param {*子缓存} son 
     * @returns 
     */
    isExpires(params,son) {
        // 如果有startTime值，说明设置了失效时间
        if ( params === null || params === undefined || !params.startTime) return params

        const date = new Date().getTime()
        if (date - params.startTime > params.expires) {
            if (!son) {
                // 缓存过期，清除缓存   
                this.removeItem(params.name)
            }
            
            return false
        } else {
            if (son) {
                return params
            }
            return JSON.parse(params.value)
        }
    }
}

export const Storage_ = new Storage()